<?php

namespace Admin\Controllers;

use Models\Supports;
use Models\Informations;
use Forms\SearchForm;

class GeneralController extends ControllerBase {

    public function indexAction() {
        
    }

    public function pageAction() {
        $aboutus = Informations::findFirstById('about');
        if ($this->request->getPost('about_change')) {
            
        }
        $this->view->aboutus = $aboutus;


        $security_rules = Informations::findFirstById('security_rules');
        $this->view->security_rules = $security_rules;

        $guide = Informations::findFirstById('user_guides');
        $this->view->guide = $guide;

        $balance_guide = Informations::findFirstById('user_money');
        $this->view->balance_guide = $balance_guide;
    }

    public function contactAction() {
        $form = new SearchForm();
        $currentPage = $this->request->getPost('page');
        if (!$currentPage) {
            $currentPage = 1;
        }
        $limit = $this->request->getPost('limit');
        if (!$limit) {
            $limit = 10;
        }
        $query = $this->modelsManager->createBuilder()
                ->columns(array(
                    'Models\Supports.id',
                    'Models\Supports.name',
                    'Models\Supports.email',
                    'Models\Supports.content',
                    'Models\Supports.created',
                    'Models\Supports.resolved',
                ))
                ->from('Models\Supports')
                ->orderby('Models\Supports.created DESC')
                ->where('1=1');

        if ($this->request->getPost('state')) {
            $query->andwhere('Models\Supports.resolved = \'' . ($this->request->getPost('state') - 1) . '\'');
        }
        if ($this->request->getPost('time')) {
            switch ($this->request->getPost('time')) {
                case '1' :
                    $query->andwhere('DATE(Models\Supports.created) = CURDATE()');
                    break;
                case '2' :
                    $query->andwhere('WEEK(Models\Supports.created) = WEEK(CURDATE())');
                    $query->andwhere('YEAR(Models\Supports.created) = YEAR(CURDATE())');
                    break;
                case '3' :
                    $query->andwhere('MONTH(Models\Supports.created) = MONTH(CURDATE())');
                    $query->andwhere('YEAR(Models\Supports.created) = YEAR(CURDATE())');
                    break;
            }
        }
        if ($this->request->getPost('text')) {
            $query->andwhere('Models\Supports.name LIKE \'%' . ($this->request->getPost('text')) . '%\'');
        }

        $supports = $query->getQuery()->execute();
        $pagination = new \Phalcon\Paginator\Adapter\Model(
                array(
            "data" => $supports,
            "limit" => $limit,
            "page" => $currentPage
                )
        );
        $this->view->supports = $pagination->getPaginate();
        $this->view->form = $form;
    }

    public function replyAction() {
        $id = $this->request->getpost('id');
        $content = $this->request->getPost('content');
        if ($id && $content) {
            $support = Supports::findFirstById($id);
            if ($support) {
                $mail_params = array(
                    'name' => $support->name,
                    'content' => $content,
                    'contact_content' => $support->content
                );
                $mail_to = array(
                    $support->email => $support->name
                );
                $this->getDI()->getMail()->send($mail_to, "Reply your contact email", 'reply', $mail_params);
                $support->resolved = 1;
                $support->save();
            }
        } else {
            echo 'Dữ liệu nhập vào chưa chính xác';
        }
        $this->view->disable();
    }

    public function deleteSupportAction() {
        $ids = $this->request->getPost('ids');
        if ($ids) {
            $ids_str = implode(',', $ids);
            $supports = Supports::find("id IN ($ids_str)");
            foreach ($supports as $support) {
                $support->delete();
            }
        }
        $this->view->disable();
    }

}
